package com.start.captcha;

import com.start.captcha.common.Fonts;
import com.start.captcha.core.AnimCaptcha;
import com.start.captcha.core.Captcha;
import com.start.captcha.support.CaptchaStyle;
import com.start.captcha.support.CaptchaType;

import java.awt.*;
import java.io.OutputStream;

/**
 * @author : start/玄狼侵袭
 * @version : 1.0
 * <p>Happy Captcha Application Class</p>
 */
public class StartCaptcha {


    private CaptchaType type;
    private CaptchaStyle style;
    private Font font;
    private int width;
    private int height;
    private int length;
    private String code;

    private OutputStream outputStream;
    private AbstractCaptcha captcha;

    public static Builder require(OutputStream outputStream) {
        return new Builder(outputStream);
    }

    public boolean finish() {
        try {
            boolean flag = false;
            if (this.style.equals(CaptchaStyle.IMG)) {
                Captcha captcha = new Captcha();

                captcha.setType(this.type);
                captcha.setWidth(this.width);
                captcha.setHeight(this.height);
                captcha.setLength(this.length);
                captcha.render(outputStream);
                this.code = captcha.getCode();
                this.captcha=captcha;
                return true;
            } else if (this.style.equals(CaptchaStyle.ANIM)) {
                AnimCaptcha captcha = new AnimCaptcha();

                captcha.setType(this.type);
                captcha.setWidth(this.width);
                captcha.setHeight(this.height);
                captcha.setLength(this.length);
                captcha.setFont(this.font);
                captcha.render(outputStream);
                this.code = captcha.getCode();
                this.captcha=captcha;
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public String getCode() {
        return code;
    }


    private StartCaptcha(Builder builder) {
        this.type = builder.type;
        this.style = builder.style;
        this.font = builder.font;
        this.width = builder.width;
        this.height = builder.height;
        this.length = builder.length;

    }

    public static class Builder {
        private CaptchaType type = CaptchaType.DEFAULT;
        private CaptchaStyle style = CaptchaStyle.IMG;
        private Font font = Fonts.getInstance().defaultFont();
        private int width = 160;
        private int height = 50;
        private int length = 5;
        private OutputStream outputStream;

        public Builder(OutputStream outputStream) {
            this.outputStream = outputStream;
        }

        public StartCaptcha build() {
            return new StartCaptcha(this);
        }

        public Builder type(CaptchaType type) {
            this.type = type;
            return this;
        }

        public Builder style(CaptchaStyle style) {
            this.style = style;
            return this;
        }

        public Builder width(int width) {
            this.width = width;
            return this;
        }

        public Builder height(int height) {
            this.height = height;
            return this;
        }

        public Builder length(int length) {
            this.length = length;
            return this;
        }

        public Builder font(Font font) {
            this.font = font;
            return this;
        }
    }

}
